<?php
namespace app\user\controller;

use app\user\controller\Base;
use think\Controller;
//use think\Controller;
use think\Request;
use app\user\model\User as UserModel;
use think\db;
use think\Session;

class Login extends Base
{
    //登录页面
    public function loginn()
    {
        return $this -> view -> fetch();
    }

    //验证登陆
    public function checkLogin(Request $request)
    {
        //初始化返回参数
        $state = 0;
        $result = '';
        $data = $request->param();
        $username=$data['name'];
        $password=md5($data['password']);
        $rule = [
            'name|用户名' => 'require',
            'password|密码' => 'require',
            'verify|验证码' => 'require|captcha',
        ];
        //自定义验证识别的提示信息
        $msg = array('name' => array('require' => '用户名不能为空,请检查您的用户名!'),
            array('password' => array('require' => '密码不能为空,请检查您的密码~')),
        );
        //validate 验证规则
        $result = $this->validate($data, $rule, $msg);
        //如果验证通过执行查询
        if ($result === true) {
            //构造查询条件
            $map = [
                'name' => $data['name'],
                'password' => md5($data['password'])
            ];
            $user = db('ole_users')->where($map)->find();

            if (!$user) {
                $result = '没有该用户,请检查';
            } else {

//                AJAX返回
                $state = 1;
                $result = '验证通过,点击[确定]后进入后台';

                Session::set('user_name',$username);
                Session::set('user_pwd',$password);
            }

        }
        //进行验证
        return ['state' => $state, 'message' => $result, 'data' => $data];

    }
}